************************************************
* IDENTITY WORKFILE
* WAVE-TO-WAVE PANEL (one observation = one person X one wave)
* FOR ANAYZING WAVE-TO-WAVE CHANGES IN IDENTITY
************************************************

clear


***********************************************************
* put together long panel data
clear
use EP_waves,replace
keep sorszam wave ABCDEF
merge 1:1 sorszam wave using EP_iwym, nogen
merge m:1 sorszam using EP_demog, nogen keepusing(female birth*)
 gen birth_ym=ym(birthy,birthmo)
 format birth_ym %tm
merge m:1 sorszam using EP_baseline_id, nogen
merge 1:1 sorszam wave using EP_roma, nogen
merge 1:1 sorszam wave using EP_parents, nogen
merge 1:1 sorszam wave using EP_hsize, nogen
merge 1:1 sorszam wave using EP_income, nogen keepusing(inc_eq_real-below)
merge 1:1 sorszam wave using EP_income_corr, nogen 
merge 1:1 sorszam wave using EP_inschool, nogen
merge 1:1 sorszam wave using EP_marriage, nogen
merge 1:1 sorszam wave using EP_newaddr_neighb, nogen
gen age_ym=(iw_ym-birth_ym)/12
gen age_y=round(age_ym)
order sorszam azon tazon wave* ABCDEF suly isuly iw_ym female age*
drop if wave=="c"
sort sorszam wave
qui compress
sort sorszam wave
saveold identification_panel,replace
*/

***********************************************************
** create workfile  with appropriate variables

** create time-invariant descent variables 
use identification_panel,replace
drop if many==0 & fany==0 & wave=="a"
drop if many==0 & fany==0 & wave=="b"

gen tempa=many==1 & fany==1 & wave=="a"
gen tempb=many==1 & fany==1 & wave=="b"
egen tempsuma=sum(tempa), by(sorszam)
egen tempsumb=sum(tempb), by(sorszam)
gen descent_twoparents=tempsuma==1 | tempsumb==1

sort sorszam wave
egen temp=rowmean(omroma mroma)
egen descent_mroma=mean(temp), by(sorszam)
 replace descent_mroma =1 if descent_mroma>0 & descent_mroma<=1
 drop temp
egen temp=rowmean(ofroma froma)
egen descent_froma=mean(temp), by(sorszam)
 replace descent_froma =1 if descent_froma>0 & descent_froma<=1
 drop temp
gen descent_roma=descent_mroma==1 | descent_froma==1 
 replace descent_roma=. if descent_mroma==. & descent_froma==. 
gen temp=mroma==1 | froma==1
 replace temp=. if mroma==. & froma==.
 egen descent_roma_narrow=mean(temp), by(sorszam)
 replace descent_roma_narrow=1 if descent_roma_narrow>0 & descent_roma_narrow<=1
 drop temp
gen temp=(mroma==1 & mbio==1) | (froma==1 & fbio==1)
 replace temp=. if mroma==. & froma==.
 egen descent_roma_bio=mean(temp), by(sorszam)
 replace descent_roma_bio=1 if descent_roma_bio>0 & descent_roma_bio<=1
 drop temp
 
gen descent_mixed= (descent_mroma==1 & descent_froma==0) | (descent_mroma==0 & descent_froma==1)
 replace descent_mixed=0 if descent_twoparents==0
gen descent_bothroma= descent_mroma==1 & descent_froma==1
 replace descent_bothroma=0 if descent_twoparents==0
*gen temp=descent_mixed+descent_bothroma
*tab descent_two temp if descent_roma==1 & wave=="f",mis
 
lab var descent_twoparents "Descent information from two parents (both mother and father in wave a or wave b)"
lab var descent_mroma "Roma descent from mother's side (mother Roma or her ancestors Roma)"
lab var descent_froma "Roma descent from father's side (father Roma or his ancestors Roma)"
lab var descent_roma "Roma descent (mother or father Roma or Roma relatives)"
lab var descent_roma_narrow "Roma descent (mother or father identified as Roma)"
lab var descent_roma_bio "Roma descent, information from biological parents only"
lab var descent_mixed "Mixed Roma descent: two parents, one with Roma descent, one w/o"
lab var descent_bothroma "Both parents of Roma descent"

* create hardship variable
gen hardship=0 if hardship_food!=. | hardship_bills!=. | hardship_heat!=.
gen hardship_intens=hardship
foreach x in food bills heat {
	replace hardship=1 if hardship_`x'==1 /* any hardship ever that year */
	replace hardship_intens=hardship_intens+(hardshipfreq_`x'/9) if hardshipfreq_`x'<=3 /* weighted by hardship intensity */
}
lab var hardship "Economic hardship that year {0,1}" 
lab var hardship_intens "Economic hardship that year, with intensity [0,1]" 

* Whether parents are biological parents 
* (when descent is measured)
cap drop temp
gen temp=mbio==1 & fbio==1 & wave=="a"
 replace temp=1 if mbio==1 & fbio==1 & wave=="b"
 egen bioparents_ab=sum(temp), by(sorszam)
 recode bioparents_ab 2=1
 lab var bioparents_ab "Lived with both biological parents in survey waves a & b"
 
* Whether lives with parents
gen woparents=many==0 & fany==0
 gen womother=many==0
 gen wofather=fany==0
 lab var woparents "Lives without parents"
 lab var womother "Lives without mother"
 lab var wofather "Lives without father"

* whether lives with partner 
gen partner_unknown=(partner_roma==. & partner==1) if partner<=1 
gen partner_nonroma=partner_roma==0 if partner<=1 
replace partner_roma=0 if partner_roma==. & partner<=1

lab var newaddress "Moved to new address since last survey wave"
lab var inschool "Enrolled in school as full-time student"
lab var child "Has child"
lab var partner "Lives with partner/spouse"
lab var partner_roma "Lives with Roma partner/spouse"
lab var partner_nonroma "Lives with non-Roma partner/spouse"
lab var partner_unkn "Lives with partner/spouse of unknown ethnicity"

order sorszam-roma roma1 roma2 descent* hardship hardship_intens inc_eq_r_corr bioparents_ab womother wofather wopar newaddress inschool child partner partner_roma partner_nonroma partner_unkn 

qui compress
saveold identification_workfile,replace

***********************************************************
* add political attitudes in wave d

use EP_attitudes
gen a_spendmore  =df5c==3 | df5c==4 if df5c<=4
gen a_excludebars=df5m==3 | df5m==4 if df5m<=4
gen a_segregate  =df5d==3 | df5d==4 if df5d<=4
gen a_demogthreat=df5g==3 | df5g==4 if df5g<=4
gen a_childforwel=df5n==3 | df5n==4 if df5n<=4
gen a_criminals  =df5k==3 | df5k==4 if df5k<=4

lab var a_spendmore   "The Roma should receive more welfare than the non-Roma (agree or strongly agree)"
lab var a_excludebars "It is fine for some bars to exlcude Roma clients (agree or strongly agree)"
lab var a_segregate   "The Roma should be segregated from the non-Roma (agree or strongly agree)"
lab var a_demogthreat "The growth of the Roma population is a threat to society (agree or strongly agree)"
lab var a_childforwel "The Roma raise children only to receive more welfare (agree or strongly agree)"
lab var a_criminals   "Criminaliry is in the blood of the Roma (agree or strongly agree)"

gen temp=1-a_spendmore
egen a_composite=rowtotal(temp a_exclude a_segreg a_demog a_child a_crim)
 replace a_composite=a_composite/6
cap drop temp
lab var a_composite "Composite index of anti-Roma political attitudes (avg of agree with negative claims)"
keep sorszam a_*
gen str1 wave="d"
qui compress
saveold temp,replace

use identification_workfile,replace
merge 1:1 sorszam wave using temp, nogen
cap drop temp*
qui compress
saveold identification_workfile,replace

